//
//  HGDividendVC.m
//  RedWinMall
//
//  Created by HeDianZhao on 2018/8/6.
//  Copyright © 2018年 HeDianZhao. All rights reserved.
//

#import "HGDividendVC.h"
#import "HGIntegralCell.h"
#import "HGDividendDetailVC.h"
#import "HGFansModel.h"

@interface HGDividendVC ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,strong)UITableView* tableView;

@property (nonatomic, assign) NSInteger page;

@property (copy, nonatomic) NSMutableArray <HGFansModel *> *rewardList;
@end

@implementation HGDividendVC

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initData];
    
    [self.tableView registerClass:[HGIntegralCell class] forCellReuseIdentifier:@"HGIntegralCell"];
}

#pragma mark -
- (void)initData {
    
    _page++;
    [[LMJRequestManager sharedManager] GET:KBaseURL(@"app/acct/flow/profit") parameters:@{@"page": [NSNumber numberWithInteger:_page]} completion:^(LMJBaseResponse *response) {
        if (response.responseModel.status == 1) {
            
            if (_page != [response.responseModel.data[@"page"] integerValue]) {
                _tableView.mj_footer.state = MJRefreshStateNoMoreData;
                return;
            }
            NSArray *models = [HGFansModel mj_objectArrayWithKeyValuesArray:response.responseModel.data[@"countDailyProfit"]];
            [self.rewardList addObjectsFromArray:models];
            
            [_tableView reloadData];
        }
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    }];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HGIntegralCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HGIntegralCell"];
    cell.fansModel = _rewardList[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _rewardList.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HGDividendDetailVC *vc = [[HGDividendDetailVC alloc] init];
    vc.dateStr = _rewardList[indexPath.row].date;
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableView *)tableView {
    if (!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 40;
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview:_tableView];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _page = 0;
            [_rewardList removeAllObjects];
            [self initData];
        }];
        
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self initData];
        }];
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(44);
            make.bottom.left.right.equalTo(self.view);
        }];
    }
    return _tableView;
}

- (NSMutableArray<HGFansModel *> *)rewardList {
    if (!_rewardList) {
        _rewardList = [@[] mutableCopy];
    }
    return _rewardList;
}


@end
